package br.com.iswe.recipemaster.gwt.recipe.client;

import br.com.iswe.gwt.crud.client.ListPanel;
import br.com.iswe.gwt.ui.client.BeanMap;
import br.com.iswe.gwt.ui.client.FormFactory;
import br.com.iswe.gwt.ui.client.ListPanelListener;
import br.com.iswe.gwt.ui.client.QueryInfo;
import br.com.iswe.gwt.ui.client.RPCService;
import br.com.iswe.recipemaster.gwt.client.RmSearchPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RecipeSearchPanel extends RmSearchPanel implements ListPanelListener {
		
	private Button selectButton;
	private Button searchButton;
	
	private RecipeListPanel listPanel;
	
	// ## IMPORTANT ## --> Define the properties that will be returned as result of the Query !!!
	// ... once configured here the method buildRow inside the ListPanel class must be changed to display the
	// properties configured here
	protected QueryInfo queryInfo = new QueryInfo(new String[]{RecipeConstants.ID_PROPERTY, RecipeConstants.NAME});
	
	
	public RecipeSearchPanel() {

		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSpacing(4);
		
		final AbsolutePanel absolutePanel = new AbsolutePanel();
		verticalPanel.add(absolutePanel);
		absolutePanel.setSize("490", "160");

		searchButton = new Button();
		absolutePanel.add(searchButton, 110, 130);
		searchButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				listPanel.search();
			}
		});
		searchButton.setWidth("112px");
		searchButton.setText("Search");

		selectButton = new Button();
		absolutePanel.add(selectButton, 278, 130);
		selectButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				selectClose();
			}
		});
		selectButton.setWidth("112px");
		selectButton.setText("Select / Close");
		
		Label lblName = new Label("Name : ");
		absolutePanel.add(lblName, 5, 31);
		
		nameTextBox = new TextBox();
		absolutePanel.add(nameTextBox, 76, 30);
		nameTextBox.setSize("379px", "22px");

		final ScrollPanel scrollPanel = new ScrollPanel();
		verticalPanel.add(scrollPanel);
		scrollPanel.setStyleName("iswe-listScrollPanel");
		scrollPanel.setSize("490", "301px");

		listPanel = new RecipeListPanel();
		scrollPanel.setWidget(listPanel);
		listPanel.setWidth("100%");
		
		setupScreen();
	}
	
	protected FormFactory formFactory = new FormFactory();
	@SuppressWarnings("unchecked")
	protected void setupScreen() {
		//configura os componentes visuais (campos) na form factory para manilupar esses dados
		
		formFactory.addProperty(RecipeConstants.NAME, nameTextBox);
		
		//formFactory.loadListBoxes(service);
		
		listPanel.setPagination(true);
		listPanel.setListPanelListener(this);
		listPanel.onSelectListener.add(onSelectListener);
		
		//default behavior
		setPopupBehavior(POPUP_BEHAVIOR_NONE);
	}

	public void setPopupBehavior(int popupBehavior){
		if(POPUP_BEHAVIOR_NONE == popupBehavior){
			selectButton.setVisible(false);
			listPanel.setCheckBoxesEnabled(false);
			listPanel.setLinkEnabled(true);
		}
		if(POPUP_BEHAVIOR_LINK == popupBehavior){
			selectButton.setText("Close");
			selectButton.setVisible(true);
			listPanel.setCheckBoxesEnabled(false);
			listPanel.setLinkEnabled(true);
		}
		if(POPUP_BEHAVIOR_CHECKBOX == popupBehavior){
			selectButton.setText("Select / Close");
			selectButton.setVisible(true);
			listPanel.setCheckBoxesEnabled(true);
			listPanel.setLinkEnabled(false);
		}
	}
	
	/**
	 * Button Select / Close
	 * close the popup and returns a list of selected Recipes
	 */
	@SuppressWarnings("unchecked")
	protected void selectClose(){
		onSelectListener.fire( listPanel.getSelectedItens() );
	}
	
	public BeanMap buildFilter() {
		return formFactory.getMapValues();
	}

	public ListPanel getListPanel() {
		return listPanel;
	}
	
	public QueryInfo getQueryInfo() {
		return queryInfo;
	}

	protected RPCService service = new RPCService(RecipeConstants.CONTROLLER_NAME);
	private TextBox nameTextBox;
	public RPCService getService() {
		return service;
	}
}
